﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Util;
namespace UtilEditors
{
    public class TransformEditers : InspectorCom<Transform>
    {
        public override void Init(SubsidiaryWindow win , int windownID)
        {
            base.Init(win , windownID);
            rect.width = 115;
            rect.height = 300;
        }
        Transform ints;
        Transform A;
        List<GameObject> B;
        bool isints;
        public override void OnIGUI()
        {
            if(Button("看着"))
            {
                component.byInvokeAll((tf) =>
                {
                    var _sceneView = SceneView.lastActiveSceneView;
                    if(_sceneView)
                    {
                        Undo.RecordObject(tf , "Look At Transform Position");
                        tf.setActive(false);
                        tf.position = _sceneView.pivot.toVector2();
                        tf.setActive(true);
                    }
                });
            }
            if(Button("加一个"))
            {
                component.byInvokeAll((tf) =>
                {
                    var n = ( new GameObject("Node_" + debug.Sole) );
                    n.transform.parent = tf;
                    n.transform.toResetLocal();
                    Undo.RegisterCreatedObjectUndo(n , "Create object");
                });
            }
            if(Button("重置位置"))
            {
                component.byInvokeAll((tf) =>
                {
                    Undo.RecordObject(tf , "Zero Transform Position");
                    tf.toResetLocal();
                });
            }
            if(Button("去除精度"))
            {
                component.byInvokeAll((tf) =>
                {
                    Undo.RecordObject(tf , "去除精度");
                    tf.localPosition = tf.localPosition.toRounds(2);
                    tf.localEulerAngles = tf.localEulerAngles.toRounds(1);
                    tf.localScale = tf.localScale.toRounds(2);
                });
            }
            if(Button("父节点同步位置"))
            {
                component.byInvokeAll((tf) =>
                {
                    var parent = tf.parent;
                    Undo.RecordObject(parent , "父节点同步位置");
                    var pos = tf.position;
                    var t = new GameObject();
                    t.transform.position = pos;

                    for(int i = parent.childCount - 1 ; i >= 0 ; i--)
                    {
                        parent.GetChild(i).parent = t.transform;
                    }
                    parent.position = pos;
                    for(int i = t.transform.childCount - 1 ; i >= 0 ; i--)
                    {
                        t.transform.GetChild(i).parent = parent;
                    }
                    GameObject.DestroyImmediate(t);
                    Undo.RecordObject(tf , "父节点同步位置");

                });
            }
            if(SelectionObj.Length >= 2)
            {
                if(Button("组合"))
                {
                    组合();
                }
            }
            if(SelectionObj.Length >= 1)
            {
                if(Button("New父节点"))
                {
                    New父节点();

                }
                if(Button("Del父节点"))
                {
                    Del父节点();
                }
            }
            排序();
            复制();
            同步();
            重命名();
            创建网格();
        }
        bool issort;
        public void 排序()
        {
            Inpt("排序" , ref issort);
            if(issort)
            {
                if(Button("选取排序"))
                {
                    component.Sort((a , b) => { return a.name.CompareTo(b.name); });
                    component.byInvokeAll((tf) =>
                    {
                        tf.SetAsFirstSibling();
                    });
                }
                if(Button("子节排序"))
                {
                    root = Selection.activeTransform;
                    List<Transform> chilist = new List<Transform>();
                    for(int i = 0 ; i < root.childCount ; i++)
                    {
                        chilist.Add(root.GetChild(i));
                    }
                    chilist.Sort((a , b) => { return a.name.CompareTo(b.name); });
                    chilist.byInvokeAll((tf) =>
                    {
                        tf.SetAsFirstSibling();
                    });
                }
            }
        }
        public bool isCopyComponent = false;
        public bool isRen = false;
        public Transform rentf;

        public void 复制()
        {
            Inpt("复制" , ref isints);
            if(isints)
            {
                Inpt("预" , ref ints);
                if(Button("复制"))
                {
                    var n = GameObject.Instantiate(ints);// (new GameObject("Node_" + debug.Sole));
                    Selection.activeGameObject = n.gameObject;
                    n.name = "Node_" + debug.Sole;
                    n.transform.parent = ints.parent;
                    n.transform.toResetLocal();

                    //Undo.RegisterCreatedObjectUndo(n, "Creates object");
                    var _sceneView = SceneView.lastActiveSceneView;
                    if(_sceneView)
                    {
                        //Undo.RecordObject(n, "Look At Transform Position");
                        n.setActive(false);
                        n.position = _sceneView.pivot.toVector2();
                        n.setActive(true);
                    }
                }
            }
        }
        public void 同步()
        {
            Inpt("同步组件" , ref isCopyComponent);
            if(isCopyComponent)
            {
                Inpt("In" , ref A);
                Inpt("Out" , ref B);
                if(Button("同步"))
                {
                    var coms = A.GetComponents<Component>();

                    for(int i = 0 ; i < coms.Length ; i++)
                    {
                        if(coms[i].isT<Transform>())
                        {
                            continue;
                        }
                        if(coms[i].isT<SpriteRenderer>())
                        {
                            continue;
                        }
                        for(int j = 0 ; j < B.Count ; j++)
                        {
                            UnityEditorInternal.ComponentUtility.CopyComponent(coms[i]);
                            var com = B[j].transform.GetOrAddComponent(coms[i].GetType());
                            UnityEditorInternal.ComponentUtility.PasteComponentValues(com);
                        }

                    }
                }
            }
        }
        public void 重命名()
        {
            Inpt("重命名" , ref isRen);
            if(isRen)
            {
                Inpt("" , ref rentf);
                if(Button("重排"))
                {
                    List<Transform> com = new List<Transform>();
                    com.Add(rentf);
                    while(com.Count > 0)
                    {
                        var n = com[0];
                        com.RemoveAt(0);
                        for(int i = 0 ; i < n.childCount ; i++)
                        {
                            var ne = n.GetChild(i);
                            ne.transform.name = ne.parent.name + ( i + 1 ).toString();
                            com.Add(ne);
                        }
                    }
                }
            }
        }
        void 组合()
        {
            var par = SelectionObj[0].transform.parent;
            var n = ( new GameObject("Node_" + debug.Sole) );
            n.transform.parent = par;
            Undo.RegisterCreatedObjectUndo(n , "Create object");
            n.transform.position = SelectionObj[0].transform.position;
            for(int i = 0 ; i < SelectionObj.Length ; i++)
            {
                Undo.SetTransformParent(SelectionObj[i].transform , n.transform , "Set 父节点");
                SelectionObj[i].transform.parent = n.transform;
            }
        }
        void New父节点()
        {
            for(int i = 0 ; i < SelectionObj.Length ; i++)
            {
                var cur = SelectionObj[i].transform;
                var par = cur.parent;
                var n = ( new GameObject(cur.name) );
                Undo.RegisterCreatedObjectUndo(n , "Create object");
                n.transform.parent = SelectionObj[i].transform;
                n.transform.toResetLocal();
                Undo.SetTransformParent(n.transform , par , "Set 父节点");
                n.transform.parent = par;
                Undo.SetTransformParent(cur , n.transform , "Set 父节点");
                cur.parent = n.transform;
            }
        }
        void Del父节点()
        {
            List<GameObject> list = new List<GameObject>();
            for(int i = 0 ; i < SelectionObj.Length ; i++)
            {
                var cur = SelectionObj[i].transform;
                var par = cur.parent;
                var parpar = par.parent;
                Undo.SetTransformParent(cur , parpar , "Set 父节点");
                cur.parent = parpar;
                if(list.IndexOf(par.gameObject) < 0)
                {
                    list.Add(par.gameObject);
                }

            }
            for(int i = 0 ; i < list.Count ; i++)
            {
                Undo.DestroyObjectImmediate(list[i].gameObject);
                GameObject.DestroyImmediate(list[i].gameObject);
            }
        }
        bool CreateGrid;
        Vector2 num;
        Vector2 offec;
        Transform root;
        public void 创建网格()
        {
            Inpt("创建网格" , ref CreateGrid);
            if(CreateGrid)
            {
                if(root == null)
                {
                    root = Selection.activeTransform;
                }
                Inpt("root" , ref root);
                Inpt("预" , ref ints);
                Inpt("num" , ref num);
                Inpt("offec" , ref offec);
                if(Button("创建"))
                {
                    int x = num.x.toInt();
                    int y = num.y.toInt();
                    for(int cy = 0 ; cy < y ; cy++)
                    {
                        for(int cx = 0 ; cx < x ; cx++)
                        {
                            var n = GameObject.Instantiate(ints);// (new GameObject("Node_" + debug.Sole));
                            n.parent = root;
                            n.name = cx + "_" + cy;
                            n.localPosition = new Vector3(cx * offec.x , -cy * offec.y , 0);
                            n.localScale = ints.localScale;
                            n.localEulerAngles = ints.localEulerAngles;
                        }
                    }
                }
            }
        }

    }
}